/**
 * Created by mengwei666 on 2016/11/5.
 */

import java.sql.*;


public class DBOpener {
    public static final String url = "jdbc:mysql://127.0.0.1/email?useSSL=true";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "1024";

    public Connection conn = null;
    public PreparedStatement pst = null;

    public Connection getConn() {
        Connection conn = null;
        try {
            Class.forName(name); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }


    public void close() {
        try {
            this.conn.close();
            this.pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public Integer sendAll(String content) {
        Connection conn = getConn();
        String sql = "select address from emaillist";
        try {
            pst = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("============================");
            while (rs.next()) {
                for (int i = 1; i <= col; i++) {
                    System.out.print(rs.getString(i) + "\t");
                    Email.sendMail(rs.getString(i),content);
                }
                System.out.println("");
            }
            System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            pst = null;
        }
        return null;
    }
}
